// 自动生成html模板并动态加载生产的xxx.bundle.js
const HtmlWebpackPlugin = require('html-webpack-plugin');
// 每次打包时清空指定文件夹
const { CleanWebpackPlugin } = require("clean-webpack-plugin");

const path = require('path')

module.exports = {
	mode: 'production',
	entry: './src/entrys/entry_lazy.js',
	output: {
		filename: '[name].lazy.bundle.js',
		path: path.resolve(__dirname, '../dists/loadLazy')
	},
	// treeShaking和代码分离配置
	optimization: {
		usedExports: true,
		splitChunks: {
			chunks: 'all'
		}
	},
	plugins: [
		new CleanWebpackPlugin({
			cleanStaleWebpackAssets: true // true清理所有文件， false只删除变动过的文件(如执行 npm run watch时dist下的index.html时没有改变的，不需要删除)
		}), // 每次打包前删除dist文件夹
		new HtmlWebpackPlugin({
			title: 'webpack懒加载',
			filename: 'lazy.html',
			template: 'src/template/template_lazy.html',
			inject: true,
			// favicon: path.join(__dirname, '..', 'favicon.ico')
		})
	]
}